//
//  ApplicationInfoViewController.m
//  XiRan-iOS
//
//  Created by 韩强 on 12/31/19.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "ApplicationInfoViewController.h"
#import "ApplicationCollectionViewCell.h"
@interface ApplicationInfoViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
/// 列表展示
@property(strong, nonatomic) UICollectionView *collectionView;



@end

@implementation ApplicationInfoViewController
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [self.view addSubview:_collectionView];
        
        [_collectionView registerClass:[ApplicationCollectionViewCell class] forCellWithReuseIdentifier:@"ApplicationCollectionViewCell"];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.view);
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)showroomArray {
    if (_showroomArray == nil) {
        _showroomArray = [NSMutableArray array];
    }
    return _showroomArray;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ApplicationCollectionViewCell";
    ApplicationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imageUrl = self.showroomArray[indexPath.row];
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
        collectionView.hidden = YES;
    } else {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
        collectionView.hidden = NO;
    }
    return self.showroomArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *datas = [NSMutableArray array];
    for (int i = 0; i < self.showroomArray.count; i++) {
        YBIBImageData *browseModel = [[YBIBImageData alloc] init];
        browseModel.allowSaveToPhotoAlbum = YES;
        browseModel.imageURL =[NSURL URLWithString:[self.showroomArray[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [datas addObject:browseModel];
    }
    
    YBImageBrowser *browser = [[YBImageBrowser alloc] init];
//    if ([MWConfigManager manager].language == 0) {
//        [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeSimplifiedChinese;
//    } else {
//        [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeEnglish;
//    }
    
    browser.defaultToolViewHandler.topView.operationType = YBIBTopViewOperationTypeSave;
    browser.dataSourceArray = datas;
    browser.currentPage = indexPath.row;
    [browser show];
}



#pragma mark - UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 11;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 16, 0, 16);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, 15);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kScreenWidth - 47) / 2, 120);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self collectionView];
    self.navigationItem.title = self.navTitleString;
}


@end
